﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace YTSK.Vouch
{
    public class VouchCount:VouchData
    {
        public VouchCount(string cvouchtype, Control c):
            base(cvouchtype, c)
        {
            base._dg.CellValidating += dataGridView1_CellValidating;
        }

        #region 数据验证
        private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
        {
            var dgc = _dg.Columns[e.ColumnIndex];
            var colname = dgc.Name;
            var list = new List<string>() { "iQuantity", "iQuoteUnitPrice", "iQuoteSum", "iDiscountSum", "iSumSale", "iQuoteMoney", "iQuoteTax","iDiscountMoney","iDiscountTax", "iTaxRateSale" };
            if (!list.Contains(colname)) { return; }

            //数量改变：金额=数量*单价；不含税金额；税额：折前金额=金额+折扣
            //单价改变：金额=数量*单价；不含税金额；税额：折前金额=金额+折扣
            //金额改变: 单价=金额/数量；不含税金额；税额：折前金额=金额+折扣
            //iQuotePrice折前金额改变：折扣=折前金额-金额
            //iDiscountPrice折扣改变：金额=折前金额-折扣

            var dgr = _dg.Rows[e.RowIndex];
            var iquantity = dgr.Cells["iQuantity"].Value.ObjToDecimal();
            var iquoteunitprice = dgr.Cells["iQuoteUnitPrice"].Value.ObjToDecimal();
            var itaxrate=dgr.Cells["iTaxRateSale"].Value.ObjToDecimal();
            var iquotesum = dgr.Cells["iQuoteSum"].Value.ObjToDecimal();
            var iquotemoney = dgr.Cells["iQuoteMoney"].Value.ObjToDecimal();
            var iquotetax = dgr.Cells["iQuoteTax"].Value.ObjToDecimal();
            var idiscountsum = dgr.Cells["iDiscountSum"].Value.ObjToDecimal();
            var idiscountmoney= dgr.Cells["iDiscountMoney"].Value.ObjToDecimal();
            var idiscounttax= dgr.Cells["iDiscountTax"].Value.ObjToDecimal();
            switch (colname)
            {
                case "iQuantity":
                    iquantity = e.FormattedValue.ObjToDecimal();
                    iquotesum = Math.Round(iquantity * iquoteunitprice, 2);
                    iquotemoney = Math.Round(iquotesum / (1 + itaxrate / 100), 2);
                    iquotetax = iquotesum - iquotemoney;
                    break;
                case "iQuoteUnitPrice":
                    iquoteunitprice = e.FormattedValue.ObjToDecimal();
                    iquotesum = Math.Round(iquantity * iquoteunitprice, 2);
                    iquotemoney = Math.Round(iquotesum / (1 + itaxrate / 100), 2);
                    iquotetax = iquotesum - iquotemoney;
                    break;
                case "iQuoteSum":
                    iquotesum = e.FormattedValue.ObjToDecimal();
                    if(iquantity!=0)
                    {
                        iquoteunitprice = iquotesum / iquantity;
                    }
                    iquotemoney = Math.Round(iquotesum / (1 + itaxrate / 100), 2);
                    iquotetax = iquotesum - iquotemoney;
                    break;
                case "iQuoteMoney":
                    iquotemoney = e.FormattedValue.ObjToDecimal();
                    iquotetax = iquotesum - iquotemoney;
                    break;
                case "iDiscountSum":
                    idiscountsum = e.FormattedValue.ObjToDecimal();
                    idiscountmoney  = Math.Round(idiscountsum / (1 + itaxrate / 100), 2);
                    idiscounttax = idiscountsum - idiscountmoney;
                    break;
                case "iDiscountMoney":
                    idiscountmoney = e.FormattedValue.ObjToDecimal();
                    idiscounttax = idiscountsum - idiscountmoney;
                    break;
                case "iTaxRateSale":
                    itaxrate = e.FormattedValue.ObjToDecimal();
                    iquotemoney = Math.Round(iquotesum / (1 + itaxrate / 100), 2);
                    iquotetax = iquotesum - iquotemoney;
                    idiscountmoney = Math.Round(iquotesum / (1 + itaxrate / 100), 2);
                    idiscounttax = idiscountsum - idiscountmoney;
                    break;                    
            }
            var isumsale = iquotesum - idiscountsum;
            var imoneysale = iquotemoney - idiscountmoney;
            var itaxsale = iquotetax - idiscounttax;
            var iunitpricesale = isumsale / iquantity;
            dgr.Cells["iQuantity"].Value = iquantity;
            dgr.Cells["iQuoteUnitPrice"].Value = iquoteunitprice;
            dgr.Cells["iTaxRateSale"].Value = itaxrate;
            dgr.Cells["iQuoteSum"].Value = iquotesum;
            dgr.Cells["iQuoteMoney"].Value = iquotemoney;
            dgr.Cells["iQuoteTax"].Value = iquotetax;
            dgr.Cells["iDiscountSum"].Value = idiscountsum;
            dgr.Cells["iDiscountMoney"].Value = idiscountmoney;
            dgr.Cells["iDiscountTax"].Value = idiscounttax;
            dgr.Cells["iSumSale"].Value = isumsale;
            dgr.Cells["iMoneySale"].Value = imoneysale;
            dgr.Cells["iTaxSale"].Value = itaxsale;
            dgr.Cells["iUnitPriceSale"].Value = iunitpricesale;
            base.sumcolAll();
        }
        #endregion
    }
}
